package com.example.spark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spark.dao.EnterDao;
import com.example.spark.entity.User;
import com.example.spark.entity.result.CommonResult;
import com.example.spark.service.EnterService;
import com.example.spark.service.minio.MinioCloudStorageService;
import org.apache.commons.io.FilenameUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import java.io.IOException;

import static com.baomidou.mybatisplus.core.toolkit.IdWorker.getId;

@Service
public class EnterServiceImpl extends ServiceImpl<EnterDao, User> implements EnterService {

    @Autowired
    private EnterDao enterDao;

    @Resource
    MinioCloudStorageService minioCloudStorageService;

    @Autowired
    private FilesServiceImpl filesService;

    //登录核对
    @Override
    public CommonResult checkLogin(String username, String password) {
        User user = enterDao.selectUser(username);
        //用户是否存在判断
        if (user != null) {
            //用户登录
            return getUserCommonResult(password, user);
        }
        return new CommonResult<>(200, "登陆失败,没有该用户");
    }

    //用户密码判断
    @NotNull
    private static CommonResult getUserCommonResult(String password, User user) {
        if (user.getPassword().equals(password)) {
            return new CommonResult<>(200, "登录成功",user.getId().toString());
        }
        return new CommonResult<>(200, "登陆失败,用户名或密码错误");
    }

    //注册
    @Override
    public CommonResult registerUser(User user) {
        String username = user.getUsername();
        User user1 = enterDao.selectUser(username);
        //雪花算法自动生成id
        long id = getId();
        user.setId(id);

        if (user1 == null) {
            enterDao.insert(user);
            return new CommonResult(200, "添加成功");
        }
        return new CommonResult(200, "添加失败");
    }


    //逻辑删除
    @Override
    public CommonResult updateUserFlag(Long id) {
        int deleted = enterDao.deleteById(id);
        if (deleted > 0) {
            filesService.deleteByUserId(id);
            return new CommonResult<>(200, "删除成功");

        } else {
            return new CommonResult<>(200, "删除失败");
        }
    }

    //图片处理
    @Override
    public CommonResult addPhoto(MultipartFile photoFile, Long id) throws IOException {

        String extension = FilenameUtils.getExtension(photoFile.getOriginalFilename());
        String url = minioCloudStorageService.uploadPhotoSuffix(photoFile.getBytes(), extension, String.valueOf(getId()));
        User user = enterDao.selectById(id);
        user.setPhoto(url);
        int is = enterDao.updateById(user);
        if (is > 0) {
            return new CommonResult(200, "添加图片成功");
        }
        return new CommonResult(200, "添加图片失败");
    }


}
